Using COM Objects

This tutorial comes in two parts, maybe three, or however many I decide to write (better yet, YOU write one and I'll post it.) This part focuses on USING COM objects in mIRC 6.03, the limitations and what just doesn't like to work.

Background

COM - Component Object Model. Its one of the things Microsoft didn't do too badly, in the grand scheme of things. The idea is to create REUSABLE, RECYCLABLE, MODULAR code. I like those words, and one day you might too. The real kicker to this is you can write a COM object in Visual Basic and use it in C++, or vice versa. Wait a moment, thats just a DLL isn't it? In a way yes, in a way no. A big part of this is the web orientation of COM objects. COM objects can be used in all sorts of places, and Microsoft's IIS server is one of them. You can use COM objects in ASP, PHP (limited), mIRC, Windows Scripting Host, Visual Studio, etc. The support is pretty damn wide spread, and there's a reason.

OOF (Object Orientated Fetishs)

In the 90's people switched from procedural programming to Object Oriented programming. This was really a boom period for win32 programming, you were no longer limited to a DOS prompt or a program that went from START to FINISH. People started thinking about event-driven programming, GUI's, and how grey is just right to make a button so bland. OOP was thrust into the spotlight, and C++ was hijacked as the language of the decade. When it was too hard to understand with all of the dropping out of C++ into C, into ASM, and back, microsoft started the visual studio series. It wasn't any easier, but now it had syntax highlighting. BASIC programmers got annoyed and hit back with Visual Basic, and before too long COM was in the spotlight. COM merely hides code behind API calls, and I've typed too much into this paragraph... :)

As a result we have OOF - people who get strangly excited about objects. Objects allow us to deal with things in an abstract manner - when you think of a dog you don't think of every atom in its body, the chemical processes of its brain etc, you go "Its black and hairy and has a leg on each corner".

Wait! I don't get the dog reference

Humans deal with things by manner of abstraction, or "simplification". We pull out a few key bits and work with them and be happy with the result. Objects were made bcause no one wants to type 300 lines of code every time they need to exit an application; they want to write it once and use it over and over. Very much like aliases. Objects have properties and methods. A property, seems self evident: My hair color is a property of mine. So is my birthday. Or age. A method, is something to do. I have a very effective kickSmallChildren() method when they get in my way. If you don't get it by now, go read a book on the subject. I also have events, such as onPain->kickSmallChildren()

Get On With It

First of all, we turn back to the help file and read it a lot. Specifically the COM bit. Look confused, then reread it. Doesn't seem too clear, does it? Think of a COM object like a supercharged remotes file - it responds to events, and has methods(aliases) & properties(identifiers). This supercharged version of aliases, etc works at a very low level (well, compared to mIRC at least). You can control applications, use public methods of applications, and make applications yell at users (controlling users). For now, lets play with excel.

Excel.Application The name of the COM object to be used. These names are registered in, funnily enough, windows registry. They have a CLSID (Class ID, funny number that looks useless), and a few other things stored there.
We want to create a new instance of Excel.Application -> We aren't running the .EXE to do it however. We simply tell mIRC to pick up a new Excel.Application as an object. In theory, we could make as many of these objects as we want. Since mIRC is handling them, you could get a result from one spreadsheet and write it in another application. Thats the power of COM here.
Once we've created it, we want to do something with it. mIRC's example sets the visibility property - but thats not very impressive, is it. Excel.Application.CheckSpelling, however might just be a little bit better. CheckSpelling is a public method definded as a subset of Excel.Application, rather than a property like visible. Which means, it does something - no I won't tell you exactly what, you'll see if you get thru this tutorial ok. What you need to do is modify the example from the mIRC help file to do the following:

  • Show Excel (Visible PROPERTY)
  • Check Spelling (Check Spelling METHOD)
  • Close Excel

    See help file for examples.







    Oh look, we're in some whitespace, aren't we?





    You don't geddit, eh? I don't blame you kiddo. So, here's a better explained example. Here's my better commented version, in that case
    excel { 
      ;Excel.Application is what the Excel com object is filed under in
      ;the WINDOWS REGISTRY. It has a clsid, which is a bunch of numbers
      ;in brackets, like: 
      ;{00000-00000-00000-0000} 
      ;That doesn't concern us yet. 
      ;Action: 
      ;Opens a new com object, and label it "excel" of type Excel.Application. 
      comopen excel Excel.Application 
    
      if ($comerr) { 
        ;if it doesn't open, give up and yell at the user 
        echo comopen failed 
        halt 
      } 
    
      ;Original: check if excel window is visible 
      ;Extended: check if excel window is visible. Do this by 
      ;accessing the public method VISIBLE in object EXCEL (opened earlier) with 
      ;1 = DISPATCH_METHOD 
      ;2 = DISPATCH_PROPERTYGET 
      ;1 + 2 = 3, which explains that bit. 
      ;That tells it to GET a return value from a METHOD 
      ;Think of it like a custom $identifier - alias monkey { return fleas } 
      ;$monkey returns the word "fleas", BUT its an alias rather than a varible. 
      ;mIRC doesn't have return types, but other langauges do. More on that later. 
    
      if ($com(excel,Visible,3) == 0) { 
        echo $com failed 
        goto finish 
      } 
    
      echo Visible: $com(excel).result 
    
      ;Original: make excel window visible 
      ;Extended: make excel window visible. Do this by 
      ;accessing the public method VISIBLE in object EXCEL (opened earlier) with 
      ;1 = DISPATCH_METHOD 
      ;4 = DISPATCH_PROPERTYPUT 
      ;That tells it to PUT(pass) a value to a METHOD 
      ;Again a custom $identifier - alias monkey { set %secret_var $1- } 
      ;$monkey(fleas) sets a varible which the user can't see (PRIVATE some might say) 
      ;this returns 0 for "we screwed up", or something else for "its all good" 
    
      if ($com(excel,Visible,5,i4,1) == 0) { 
        echo $com failed 
        goto finish 
      } 
    
      ; check visibility again 
      ;look above 
      if ($com(excel,Visible,3) == 0) { 
        echo $com failed 
        goto finish 
      } 
    
      echo Visible: $com(excel).result 
    
      ;Here's the spell checking code
      ;well, at least part of it
      ;
      ;if ($com(excel,CheckSpelling,1,string,word) == 0) { 
      ;  echo $com failed 
      ;  goto finish 
      ;} 
      ;echo -s Is Spelled Correctly?: $com(excel).result
    
      :finish 
      ;get rid of the stupid thing, we are sick of it. 
      comclose excel 
    }
    

    The Bee's Are Doing What Now?

    That all wonderful, if you got it to work. But how do you find the methods? Well its simple enough if you have any MS Office application (Word, Excel, etc), or any of the Visual Basic 6 IDE. There is something called an 'object browser'. The object browser exposes all public methods and properties of a COM object or a DLL, allowing you to browse thru and find out what's going on.

    I'll let you find your own start menus, but in the MS Office Suite, look at your "tools" menu, and find the "visual basic editor". From there, F2 should bring up your object browser. Make sure you add a reference to your component, otherwise the obj. browser won't know what its meant to look at.

    Thats About All For This First Part

    2nd installment: Using WSH to manipulate COM

    3rd installment: Creating COM objects under WSH